package com.ruoyi.aimodel.controller;

import com.ruoyi.aimodel.domain.AipptOutline;
import com.ruoyi.aimodel.service.DeepSeekService;
import com.ruoyi.aimodel.service.EduAiService;
import com.ruoyi.aimodel.service.ZhipuService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.UUID;
import java.util.logging.Logger;

@RestController
@RequestMapping("/api")
public class AiController {
    private static final Logger logger = Logger.getLogger(AiController.class.getName());
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(AiController.class);

    @Autowired
    private ZhipuService zhipuService;
    @Autowired
    private DeepSeekService deepSeekService;
    @Autowired
    private EduAiService eduAiService;

    @PostMapping(value = "/modelChatStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter modelChatStream(@RequestBody AipptOutline aipptOutline) {
        SseEmitter emitter = new SseEmitter(180000L);
        if(aipptOutline.getUserid().isEmpty()||aipptOutline.getSession().equals("0")){
            log.info("userid为空或关闭会话，开启临时会话");
            aipptOutline.setUserid(UUID.randomUUID().toString());
        }

        try {
            if (aipptOutline.getModel().equals("deepseek")){
                deepSeekService.callDeepSeekStream(aipptOutline.getUserid(), aipptOutline.getContent())
                        .subscribe(
                                content -> {
                                    try {
                                        emitter.send(content, MediaType.TEXT_PLAIN);
                                    } catch (IOException e) {
                                        logger.severe("发送内容错误: " + e.getMessage());
                                        emitter.completeWithError(e);
                                    }
                                },
                                error -> {
                                    try {
                                        logger.severe("处理出现异常: " + error.getMessage());
                                        emitter.send(SseEmitter.event().data("Error: " + error.getMessage()));
                                    } catch (IOException e) {
                                        logger.severe("发送了异常信息: " + e.getMessage());
                                        emitter.completeWithError(e);
                                    } finally {
                                        emitter.complete();
                                    }
                                },
                                () -> {
                                    emitter.complete();
                                }
                        );
            }else if(aipptOutline.getModel().equals("doubao")){
                eduAiService.getDoubaoStream(aipptOutline.getUserid(), aipptOutline.getContent())
                        .subscribe(
                                content -> {
                                    try {
                                        emitter.send(content, MediaType.TEXT_PLAIN);
                                    } catch (IOException e) {
                                        logger.severe("发送内容错误: " + e.getMessage());
                                        emitter.completeWithError(e);
                                    }
                                },
                                error -> {
                                    try {
                                        logger.severe("处理出现异常: " + error.getMessage());
                                        emitter.send(SseEmitter.event().data("Error: " + error.getMessage()));
                                    } catch (IOException e) {
                                        logger.severe("发送了异常信息: " + e.getMessage());
                                        emitter.completeWithError(e);
                                    } finally {
                                        emitter.complete();
                                    }
                                },
                                () -> {
                                    emitter.complete();
                                }
                        );
            }else if(aipptOutline.getModel().equals("zhipu")){
                zhipuService.callZhipuStream(aipptOutline.getUserid(), aipptOutline.getContent())
                        .subscribe(
                                content -> {
                                    try {
                                        emitter.send(content, MediaType.TEXT_PLAIN);
                                    } catch (IOException e) {
                                        logger.severe("发送内容错误: " + e.getMessage());
                                        emitter.completeWithError(e);
                                    }
                                },
                                error -> {
                                    try {
                                        logger.severe("处理出现异常: " + error.getMessage());
                                        emitter.send(SseEmitter.event().data("Error: " + error.getMessage()));
                                    } catch (IOException e) {
                                        logger.severe("发送了异常信息: " + e.getMessage());
                                        emitter.completeWithError(e);
                                    } finally {
                                        emitter.complete();
                                    }
                                },
                                () -> {
                                    emitter.complete();
                                }
                        );
            }

        } catch (Exception e) {
            try {
                logger.severe("Unexpected error: " + e.getMessage());
                emitter.send(SseEmitter.event().data("Error: " + e.getMessage()));
            } catch (IOException ex) {
                logger.severe("Error sending unexpected error message: " + ex.getMessage());
                emitter.completeWithError(ex);
            } finally {
                emitter.complete();
            }
        }

        return emitter;
    }

    @PostMapping("/modelChat")
    public String modelChat(@RequestBody AipptOutline aipptOutline) {
        if(aipptOutline.getUserid().isEmpty()||aipptOutline.getSession().equals("0")){
            log.info("userid为空或关闭会话，开启临时会话");
            aipptOutline.setUserid(UUID.randomUUID().toString());
        }
        String resultMsg="";
        if (aipptOutline.getModel().equals("deepseek")){
            resultMsg=deepSeekService.callDeepSeek(aipptOutline.getUserid(), aipptOutline.getContent());

        }else if(aipptOutline.getModel().equals("doubao")){
            resultMsg=eduAiService.getDoubaoChat(aipptOutline.getUserid(), aipptOutline.getContent());

        }else if(aipptOutline.getModel().equals("zhipu")){
            resultMsg=zhipuService.callZhipu(aipptOutline.getUserid(), aipptOutline.getContent());

        }
        log.info(resultMsg);
        return resultMsg;
    }




}